#import "layer_library_credits.h"

#define CREDITS_TITLE_X 50.0f
#define CREDITS_CONTEXT_X 100.0f
#define CREDITS_LINE_SPLITE 5.0f
#define CREDITS_MOVE_SPEED 1.0f

#define CREDITS_FONT_NAME @"font_treasure.ttf"

#if (HARD_WARE == HARD_WARE_IPHONE)
	#define CREDITS_TITLE_SIZE 16
#elif (HARD_WARE == HARD_WARE_IPAD)
	#define CREDITS_TITLE_SIZE 24
#endif

#if (HARD_WARE == HARD_WARE_IPHONE)
	#define CREDITS_CONTEXT_SIZE 24
#elif (HARD_WARE == HARD_WARE_IPAD)
	#define CREDITS_CONTEXT_SIZE 48
#endif

@implementation layer_library_credits

- (id)initLibraryCredits
{
	CCSprite *_Background;
	float _TotalW;
	float _StartX;

	if ((self = [super init]))
	{
		[self schedule:@selector(tick:)];

		self.isTouchEnabled = YES;

		/*backgoround;
		*/
		_Background = uiNodeAddImage(self, [NSString stringWithUTF8String:"library_credits_bg.png"], 0, g_ScrrenCenter);
		_Background.scale = 2.0f;

		/*text
		*/
		startY_ = 0.0f;
		[self CreateTitleText:"Produce by"];
		[self CreateContextText:"Lonicera Studio"];
		[self CreateTitleText:"Designed by"];
		[self CreateContextText:"Kykiske Zhang"];
		[self CreateContextText:"Zappa Bao"];
		[self CreateTitleText:"Program by"];
		[self CreateContextText:"tonysyata"];
		[self CreateTitleText:"Music by"];
		[self CreateContextText:"Zappa Bao"];
		[self CreateTitleText:"Arts by"];
		[self CreateContextText:"Qingfeng Luo"];
		[self CreateContextText:"Chunjiang Zhou"];
		[self CreateContextText:"Wuji Xu"];
		[self CreateContextText:"Junfeng Zhang"];
		[self CreateContextText:"Yufeng Yang"];
		[self CreateTitleText:"Special thanks"];
		[self CreateTitleText:"Wuhan World Digital Technology Co.Ltd"];

		/*logo
		*/
		logo5Dong_ = [CCSprite spriteWithFile:@"library_credits_logo_5dong.png"];
		[self addChild:logo5Dong_];
		logo5Dong_.anchorPoint = ccp(0.0f, 0.5f);

		_TotalW = textTable_.back().contentSizeInPixels.width + logo5Dong_.contentSizeInPixels.width + 20;
		_StartX = g_ScrrenSize.width / 2.0f - _TotalW / 2.0f;
		textTable_.back().positionInPixels = ccp(_StartX, textTable_.back().positionInPixels.y);
		logo5Dong_.positionInPixels = ccp(
		    textTable_.back().positionInPixels.x + textTable_.back().contentSizeInPixels.width + 20,
		    textTable_.back().positionInPixels.y - textTable_.back().contentSizeInPixels.height / 2.0f);

		logoLonicera_ = [CCSprite spriteWithFile:@"library_credits_logo_lonicera.png"];
		[self addChild:logoLonicera_];
		logoLonicera_.anchorPoint = ccp(0.5f, 0.5f);
		logoLonicera_.positionInPixels = ccp(
		    g_ScrrenCenter.x,
		    logo5Dong_.positionInPixels.y - (logo5Dong_.contentSizeInPixels.height + g_ScrrenSize.height + logoLonicera_.contentSizeInPixels.height) / 2.0f);
	}
	return self;
}

- (void)tick:(ccTime)Time
{
	unsigned int i;
	float _Speed;

	if (g_ScrrenCenter.y < logoLonicera_.positionInPixels.y + CREDITS_MOVE_SPEED)
	{
		_Speed = g_ScrrenCenter.y - logoLonicera_.positionInPixels.y;
		[self unschedule:@selector(tick:)];
	}
	else
	{
		_Speed = CREDITS_MOVE_SPEED;
	}
	for (i = 0; i < textTable_.size(); i++)
	{
		textTable_[i].positionInPixels = ccp(textTable_[i].positionInPixels.x, textTable_[i].positionInPixels.y + _Speed);
	}
	logo5Dong_.positionInPixels = ccp(logo5Dong_.positionInPixels.x, logo5Dong_.positionInPixels.y + _Speed);
	logoLonicera_.positionInPixels = ccp(logoLonicera_.positionInPixels.x, logoLonicera_.positionInPixels.y + _Speed);
}

- (void)ccTouchesEnded:(NSSet *)Touch withEvent:(UIEvent *)Event
{
	[g_SceneLibrary SetState:SCENE_LIBRARY_STATE_MAIN];
}

- (CCLabelTTF *)CreateTitleText:(char const *)String
{
	CCLabelTTF *_Text;

	_Text = [CCLabelTTF labelWithString:[NSString stringWithUTF8String:String] fontName:CREDITS_FONT_NAME fontSize:CREDITS_TITLE_SIZE];
	[self addChild:_Text];
	_Text.anchorPoint = ccp(0.0f, 1.0f);
	_Text.positionInPixels = ccp(CREDITS_TITLE_X, startY_);
	startY_ -= _Text.contentSizeInPixels.height + CREDITS_LINE_SPLITE;
	textTable_.push_back(_Text);
	return _Text;
}

- (CCLabelTTF *)CreateContextText:(char const *)String
{
	CCLabelTTF *_Text;

	_Text = [CCLabelTTF labelWithString:[NSString stringWithUTF8String:String] fontName:CREDITS_FONT_NAME fontSize:CREDITS_CONTEXT_SIZE];
	[self addChild:_Text];
	_Text.anchorPoint = ccp(0.0f, 1.0f);
	_Text.positionInPixels = ccp(CREDITS_CONTEXT_X, startY_);
	startY_ -= _Text.contentSizeInPixels.height + CREDITS_LINE_SPLITE;
	textTable_.push_back(_Text);
	return _Text;
}

@end